package com.lanxin.oa.basic.controller;

import com.lanxin.bean.JcBasicdatavalue;
import com.lanxin.oa.basic.service.BasicService;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/basic")
public class BasicController {
    @Autowired
    private BasicService basicService;

    @RequestMapping("/selectBasic")
    public ModelAndView selectBasic(Integer page, Integer pageSize, JcBasicdatavalue basic) {
        PageBean<JcBasicdatavalue> pageBean = basicService.getJcBasicPage(page, pageSize, basic);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageBean", pageBean);
        mv.addObject("basic", basic);
        mv.setViewName("../view/basic/jc.jsp");
        return mv;
    }

    @RequestMapping("/toAddBasicdata")
    public ModelAndView toAddBasicdata() {
        ModelAndView mv = new ModelAndView();
        List<JcBasicdatavalue> listbasic = basicService.selectBasic();
        mv.addObject("listbasic", listbasic);
        mv.setViewName("../view/basic/editbasic.jsp");
        return mv;
    }

    @RequestMapping("/editBasicdata")
    public ModelAndView editBasicdata(JcBasicdatavalue basic) {
        ModelAndView mv = new ModelAndView();
        basicService.editBasic(basic);
        mv.setViewName("redirect:/basic/selectBasic");
        return mv;
    }

    @RequestMapping("/toUpdateBasicdata")
    public ModelAndView toUpdateBasicdata(Integer oid) {
        ModelAndView mv = new ModelAndView();
        JcBasicdatavalue basic = basicService.selectBasicById(oid);
        List<JcBasicdatavalue> listbasic = basicService.selectBasic();
        mv.addObject("basic", basic);
        mv.addObject("listbasic", listbasic);
        mv.setViewName("../view/basic/editbasic.jsp");
        return mv;
    }

    @RequestMapping("/checkBasicdataName")
    @ResponseBody
    public Map<String, Boolean> checkBasicdataName(String name, Integer oid) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        boolean b = basicService.selectBasicByName(name, oid);
        if (!b) {
            map.put("valid", true);
            return map;
        }
        map.put("valid", false);
        return map;
    }

    @RequestMapping("/deleteBasic")
    @ResponseBody
    public boolean deleteBasic(JcBasicdatavalue basic) {
        boolean b = basicService.delete(basic);
        return b;
    }
}
